package department.fc.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import department.fc.common.AttributeAndView;
import department.fc.common.Controller;
import department.fc.domain.Department;
import department.fc.service.DepartmentService;

public class RegisterDepartmentController implements Controller{

	@Override
	public AttributeAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		int departmentNum = Integer.parseInt(request.getParameter("departmentNum"));
		String departmentName = request.getParameter("departmentName");
		String chiefName = request.getParameter("chiefName");
		int departmentNumOfPeople = Integer.parseInt(request.getParameter("departmentNumOfPeople"));
		String location = request.getParameter("location");
		
		Department department = new Department(departmentNum, departmentName, chiefName, departmentNumOfPeople, location);
		
		AttributeAndView av = null;
		
		DepartmentService service = DepartmentService.getInstance();
		
		try {
			service.registerDepartment(department);
			av = new AttributeAndView(false, "/WEB-INF/jsp/dept/department_detail.jsp", "department", department);
			
		} catch (SQLException e) {
			e.printStackTrace();
			av = new AttributeAndView(false, "WEB-INF/jsp/error.jsp", "error_message", e.getMessage());
		}
		return av;

	}

}
